package com.ctjsoft.competition;

/**
 * 模拟抢票
 * 10张票，3个线程去抢，出现资源竞争现象，导致重复票出现
 * @author liaoyy
 * @date Created in 2022/1/8 16:14
 */
public class TestUnSafeBuyTickets implements Runnable {

    private static int num = 10;
    private static boolean flag = true;

    @Override
    public void run() {
        while(flag) {
            if (num <= 0) {
                flag = false;
                return;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "拿到了第" + num-- + "张票");
        }

        }


    public static void main(String[] args) {
        TestUnSafeBuyTickets testRunnable = new TestUnSafeBuyTickets();
        new Thread(testRunnable, "xiaoming").start();
        new Thread(testRunnable, "xiaowang").start();
        new Thread(testRunnable, "xiaoli").start();
    }

}
